home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / scnoise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-22  |  1.6 KB  |  70 lines

  1. #include "proctext.h"
  2. #include "noise.h"
  3.  
  4. #define NEXT(h)     (((h)+1) & TABMASK)
  5. #define NIMPULSES   3
  6.  
  7. static float impulseTab[TABSIZE*4];
  8.  
  9. static void impulseTabInit(int seed);
  10.  
  11. float
  12. scnoise(float x, float y, float z)
  13. {
  14.     static int initialized;
  15.     float *fp;
  16.     int i, j, k, h, n;
  17.     int ix, iy, iz;
  18.     float sum = 0;
  19.     float fx, fy, fz, dx, dy, dz, distsq;
  20.  
  21.     /* Initialize the random impulse table if necessary. */
  22.     if (!initialized) {
  23.         impulseTabInit(665);
  24.         initialized = 1;
  25.     }
  26.  
  27.     ix = FLOOR(x); fx = x - ix;
  28.     iy = FLOOR(y); fy = y - iy;
  29.     iz = FLOOR(z); fz = z - iz;
  30.     
  31.     /* Perform the sparse convolution. */
  32.     for (i = -2; i <= 2; i++) {
  33.       for (j = -2; j <= 2; j++) {
  34.         for (k = -2; k <= 2; k++) {
  35.             /* Compute voxel hash code. */
  36.             h = INDEX(ix+i,iy+j,iz+k);
  37.             
  38.             for (n = NIMPULSES; n > 0; n--, h = NEXT(h)) {
  39.                 /* Convolve filter and impulse. */
  40.                 fp = &impulseTab[h*4];
  41.                 dx = fx - (i + *fp++);
  42.                 dy = fy - (j + *fp++);
  43.                 dz = fz - (k + *fp++);
  44.                 distsq = dx*dx + dy*dy + dz*dz;
  45.                 sum += catrom2(distsq) * *fp;
  46.             }
  47.         }
  48.       }
  49.     }
  50.  
  51.     return sum / NIMPULSES;
  52. }
  53.  
  54. static void
  55. impulseTabInit(int seed)
  56. {
  57.     int i;
  58.     float *f = impulseTab;
  59.     extern long srandom();
  60.     extern long random();
  61.  
  62.     srandom(seed); /* Set random number generator seed. */
  63.     for (i = 0; i < TABSIZE; i++) {
  64.         *f++ = RANDNBR;
  65.         *f++ = RANDNBR;
  66.         *f++ = RANDNBR;
  67.         *f++ = 1. - 2.*RANDNBR;
  68.     }
  69. }
  70.